package com.mcintyret.site.journal;

import com.mcintyret.site.users.OwnedByUser;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.*;
import java.util.LinkedList;
import java.util.List;

/**
 * User: mcintyret2
 * Date: 28/03/2012
 */
@Entity
@Table(name = "journal_families")
public class JournalFamily implements OwnedByUser {

    private Integer id;
    private String name;
    private String username;
    private String description;
    private int order;
    private List<Journal> journals = new LinkedList<Journal>();

    // For use by Hibernate
    JournalFamily() {}

    public JournalFamily(int order, String username, String name) {
        this(order, username, name, null);
    }

    public JournalFamily(int order, String username, String name, String description) {
        setOrder(order);
        setName(name);
        setUsername(username);
        setDescription(description);
    }

    @Id
    @GeneratedValue
    @Column(name = "journal_family_id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "order_num")
    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public JournalFamily addJournal(Journal journal) {
        journals.add(journal);
        journal.setJournalFamily(this);
        journal.setOrder(journals.size());
        journal.setUsername(username);
        return this;
    }

    public void setJournals(List<Journal> journals) {
        this.journals = journals;
    }

    @OneToMany(orphanRemoval = true, mappedBy = "journalFamily")
    @OrderBy("order")
    @Cascade(CascadeType.ALL)
    public List<Journal> getJournals() {
        return journals;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        JournalFamily that = (JournalFamily) o;

        if (order != that.order) {
            return false;
        }
        if (description != null ? !description.equals(that.description) : that.description != null) {
            return false;
        }
        if (name != null ? !name.equals(that.name) : that.name != null) {
            return false;
        }
        if (username != null ? !username.equals(that.username) : that.username != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + order;
        return result;
    }
}
